package com.mallcai.bigdata.ladon.dpl.mapper.system.provider;

import com.mallcai.bigdata.ladon.dpl.entity.system.Role;
import com.mallcai.bigdata.ladon.dpl.mapper.BaseSqlProvider;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.jdbc.SQL;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author gst
 * @date 2019-12-30 20:36
 */
public class RoleSqlProvider extends BaseSqlProvider<Role> {

    public static final String TB_ROLE = "sys_user_role";

    /**
     * 获取表名
     *
     * @return String
     */
    @Override
    public String getTableName() {
        return TB_ROLE;
    }

    /**
     * 获取插入数据的字段列表
     *
     * @return Map
     */
    @Override
    public Map<String, String> getInsertColumns() {
        Map<String, String> map = new LinkedHashMap<>();
        map.put("name", "#{name}");
        map.put("code", "#{code}");
        map.put("type", "#{type}");
        map.put("`desc`", "#{desc}");
        return map;
    }

    /**
     * 获取更新操作的字段列表
     *
     * @return Map
     */
    @Override
    public Map<String, String> getUpdateColumns() {
        Map<String, String> map = new LinkedHashMap<>();
        map.put("name", "name");
        map.put("code", "code");
        map.put("type", "type");
        map.put("`desc`", "`desc`");
        return map;
    }

    public String getByParams(Map<String, Object> map) {
        Map<String, Object> params = getParams(map);

        StringBuilder sb = new StringBuilder();
        sb.append("archive = 0");
        if (params.containsKey("name") && StringUtils.isNotBlank(params.get("name").toString())) {
            sb.append(" and name like concat('%', #{params.name}, '%')");
        }
        if (params.containsKey("code") && StringUtils.isNotBlank(params.get("code").toString())) {
            sb.append(" and code like concat('%', #{params.code}, '%')");
        }
        if (params.containsKey("type")) {
            sb.append(" and type = #{params.type}");
        }
        return new SQL() {
            {
                SELECT("*");
                FROM(getTableName());
                WHERE(sb.toString());
            }
        }.toString();
    }
}
